# Maintainer: Jakub Panek <me@panekj.dev>
pkgname=wezterm
pkgver=20220624.141144
_pkgver="20220624-141144-bd1b7c5d"
pkgrel=0
pkgdesc="GPU-accelerated cross-platform terminal emulator and multiplexer written in Rust"
url="https://wezfurlong.org/wezterm/"
# s390x, riscv64 limited by rust
# ppc64le limited by ring https://github.com/briansmith/ring/issues/389
# armv7 armhf x86 limited by 32-bit incompatibility
arch="x86_64 aarch64"
license="MIT"
options="net"
makedepends="
	cargo
	fontconfig-dev
	freetype-dev
	harfbuzz-dev
	libpng-dev
	libssh2-dev
	libx11-dev
	libxkbcommon-dev
	ncurses
	openssl-dev
	python3
	wayland-dev
	xcb-util-dev
	xcb-util-image-dev
	xcb-util-keysyms-dev
	xcb-util-wm-dev
	zlib-dev
	zstd-dev
	"
checkdepends="openssh"
subpackages="$pkgname-extra-terminfo:_extra_terminfo:noarch"
source="
	https://github.com/wez/wezterm/releases/download/$_pkgver/wezterm-$_pkgver-src.tar.gz
	link-against-system-libs.patch
	remove-update-checker.patch
	"
builddir="$srcdir/wezterm-$_pkgver"

export CARGO_PROFILE_RELEASE_LTO="true"
export CARGO_PROFILE_RELEASE_PANIC="abort"
export CARGO_PROFILE_RELEASE_OPT_LEVEL="s"
export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1

prepare() {
	default_prepare

	# dont print malformed sshd config, this can possibly fail on builders but not on CI
	sed 's|std::fs::write|//std::fs::write|g' -i "$builddir"/wezterm-ssh/tests/sshd.rs
	cargo fetch --locked
}

build() {
	cargo build --release --frozen --manifest-path wezterm/Cargo.toml
	cargo build --release --frozen --manifest-path wezterm-gui/Cargo.toml
	cargo build --release --frozen --manifest-path wezterm-mux-server/Cargo.toml
	cargo build --release --frozen --manifest-path strip-ansi-escapes/Cargo.toml

	tic -x -o "$builddir"/terminfo "$builddir"/termwiz/data/wezterm.terminfo
}

check() {
	cargo test --frozen

	# required to unstuck CI
	killall -q sshd || true
}

package() {
	depends="$pkgname-extra-terminfo=$pkgver=r$pkgrel"

	cargo install --frozen --offline --root="$pkgdir"/usr --path "$builddir"/wezterm
	cargo install --frozen --offline --root="$pkgdir"/usr --path "$builddir"/wezterm-gui
	cargo install --frozen --offline --root="$pkgdir"/usr --path "$builddir"/wezterm-mux-server
	cargo install --frozen --offline --root="$pkgdir"/usr --path "$builddir"/strip-ansi-escapes

	install -Dm644 -t "$pkgdir"/usr/share/applications "$builddir"/assets/wezterm.desktop
	install -Dm644 -t "$pkgdir"/usr/share/metainfo "$builddir"/assets/wezterm.appdata.xml
	install -Dm644 -t "$pkgdir"/usr/share/licences/wezterm "$builddir"/LICENSE.md
	install -Dm644 -t "$pkgdir"/etc/profile.d "$builddir"/assets/shell-integration/wezterm.sh
	install -Dm644 "$builddir"/assets/icon/terminal.png "$pkgdir"/usr/share/pixmaps/wezterm.png
	install -Dm644 "$builddir"/assets/icon/wezterm-icon.svg "$pkgdir"/usr/share/pixmaps/wezterm.svg
	install -Dm644 "$builddir"/assets/icon/terminal.png "$pkgdir"/usr/share/icons/hicolor/128x128/apps/wezterm.png
	install -Dm644 "$builddir"/assets/icon/wezterm-icon.svg "$pkgdir"/usr/share/icons/hicolor/scalable/apps/wezterm.svg
	install -Dm644 "$builddir"/terminfo/w/wezterm "$pkgdir"/usr/share/terminfo/w/wezterm

	rm "$pkgdir"/usr/.crates*
}

_extra_terminfo() {
	pkgdesc="$pkgdesc (extra terminfo data)"
	depends="ncurses-terminfo-base"

	amove usr/share/terminfo/w/wezterm
}

sha512sums="
b07989b9b0125616744300f2c1311b8ec0d36cc1ac21a472ed13deb1bbcb6c1d48c9852b07a3d4a851e9e5b8548225cb930b52834249fe2ab0b3955605ab65aa  wezterm-20220624-141144-bd1b7c5d-src.tar.gz
8eca5eec73c1d1be93cc5e1d9e7e04935812baba5872781f242959b33c483c5d12c05eb807304e3a00b9fc078314d5b74b81fee709328e84fd4f55cb857493bd  link-against-system-libs.patch
f0ff414cd61acc921921faef214c2ab8cb2e1a5e84da64c69d5cc17ad77b001fbd4d2550d5653289b91395430b3210911edc83f94105e8fc0cc23c111f322434  remove-update-checker.patch
"
